<template>
	<view class="content">
		<uinput v-model="password" placeholder="请输入新密码" type="password" labelPosition="top" label="新密码"></uinput>
		<uinput v-model="password_2" placeholder="确认新密码" type="password" labelPosition="top" label="确认新密码"></uinput>
		<button type="default" @click="updatePassword()">确认修改</button>
		<text style="color: red;">{{error_text}}</text>
	</view>
</template>

<script>
	import uinput from "../../components/poiuy-uinput/uinput.vue";
	export default {
		components: {
			uinput
		},
		data() {
			return {
				password: "",
				password_2: "",
				error_text: "",
			}
		},
		onLoad() {
			this.password = "";
			this.password_2 = "";
			this.error_text = "";
		},
		methods: {
			updatePassword() {
				var _self = this
				if (this.password == this.password_2 && this.password != "") {
					uni.getStorage({
						key: 'token',
						success: function(storage) {
							uni.request({
								url: "http://localhost:4567/user_json/updatePassword.opera",
								method: "POST",
								header: {
									"token": storage.data.tokenName,
								},
								data: {
									"password": _self.password,
									"password_2": _self.password_2,
								},
								success: (res) => {
									if (res.data.code) {
										_self.error_text = res.data.msg;
									} else {
										uni.navigateTo({
											url: '/pages/login/login'
										});
									}
								}
							})
						},
						fail: function(res) {
							_self.error_text = "服务器错误";
						}
					});
				} else {
					this.error_text = "密码不为空或两次输入密码不匹配";
				}
			}
		}
	}
</script>

<style>

</style>
